package com.fm.websocket;

import com.fm.mqtt.MqttPushClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;

@Controller
public class NotifyController {
	
	private static final Logger logger = LoggerFactory.getLogger(NotifyController.class);
	
	@Autowired
	private SimpMessagingTemplate template;

	@MessageMapping("/notify")
    public void notify(@RequestBody WebChatMessage message) {
        template.convertAndSend("/topic/" + message.getTo(), message.getContent());
    }

	/**
	 * 接收自mqtt的消息
	 * @param revice
	 */
    @MessageMapping("/mqtt")
	public void reviceMqtt(String revice) {
		String res [] = revice.split("-T-");
		template.convertAndSend(res[0], res[1]);
	}

	/**
	 * 接收自ws的消息
	 * @param message
	 */
	@MessageMapping("/tomqtt")
	public void toMqttSys(@RequestBody SendToMqttMessage message) {
    	System.out.println(message.getTopic());
		MqttPushClient.getInstance().publish(message.getTopic(), message.getMessage(), 0);
	}
}
